package com.sk.weichat.ui.mucfile.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import com.alibaba.android.vlayout.layout.GridLayoutHelper;
import com.bumptech.glide.Glide;
import com.sk.weichat.R;
import com.sk.weichat.ui.mucfile.bean.FileBean;

import java.util.List;

public class MediaImageAdapter2 extends DelegateAdapter.Adapter {
    public interface OnImageClickListener {
        void onClick(FileBean fileBean);
    }

    private OnImageClickListener listener;

    public void setOnImageClickListener(OnImageClickListener listener) {
        this.listener = listener;
    }

    public List<FileBean> getData() {
        return list;
    }

    Context context;
    List<FileBean> list;

    public MediaImageAdapter2(Context context, List<FileBean> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public LayoutHelper onCreateLayoutHelper() {
        GridLayoutHelper helper = new GridLayoutHelper(4);
        helper.setHGap(0);
        helper.setVGap(0);
        helper.setAutoExpand(false);
        return helper;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_media_image, null);
        return new ImageHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
        ImageHolder holder = (ImageHolder) viewHolder;
        Glide.with(context).load(list.get(i).getFile()).centerCrop().placeholder(R.mipmap.default_error).error(R.mipmap.default_error).dontAnimate().into(holder.ivImage);
        //选择
        if (list.get(i).isChecked()) {
            holder.ivCheck.setImageResource(R.mipmap.icon_checked);
        } else {
            holder.ivCheck.setImageResource(R.mipmap.icon_checkedbox_normal);
        }
        holder.ivCheck.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != v) {
                    listener.onClick(list.get(i));
                }
            }
        });
        holder.ivImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != v) {
                    listener.onClick(list.get(i));
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    class ImageHolder extends RecyclerView.ViewHolder {
        ImageView ivImage;
        ImageView ivCheck;

        public ImageHolder(@NonNull View itemView) {
            super(itemView);
            ivImage = itemView.findViewById(R.id.iv_image);
            ivCheck = itemView.findViewById(R.id.iv_check);
        }
    }
}
